#include "medical.h"
#include "ui_medical.h"

Medical::Medical(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Medical)
{
    ui->setupUi(this);
    QFont font("Microsoft YaHei",12,75);
    QFont font_line("宋体",12,30);
    Warning_State=1;
    this->setWindowTitle(tr("医护人员显示界面"));
//####################
//创建一个Qcomobox部件和lable用来显示病床号那一行
//###################
    //创建一个lable
    Bed_Nub=new QLabel(this);
    Bed_Nub->setText(QObject::tr("病床号:"));
    Bed_Nub->setGeometry(10,10,100,30);
    Bed_Nub->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Bed_Nub->setFont(font);
    Bed= new QComboBox(this);//作为该界面的部件而不是窗口
    Bed->addItem(QObject::tr("21-2-3"));
    Bed->addItem(QObject::tr("34-2-1"));//添加选项
    //    sex->resize(100,30);使用下面的函数即可
    Bed->setGeometry(110,10,120,30);//设置部件的位置，长宽高和起始坐标
    Bed->setFont(font_line);
//####################
//创建姓名那一行
//###################
    name=new QLabel(this);
    name->setText(QObject::tr("姓名:"));
    name->setGeometry(10,110,100,30);
    name->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    name->setFont(font);

    name_value=new QLabel(this);
    name_value->setText(QObject::tr("XXX"));
    name_value->setGeometry(110,110,100,30);
    name_value->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    name_value->setFont(font_line);

    Bed_Num_lable=new QLabel(this);
    Bed_Num_lable->setText(QObject::tr("病床号:"));
    Bed_Num_lable->setGeometry(210,110,100,30);
    Bed_Num_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Bed_Num_lable->setFont(font);

    Bed_value=new QLabel(this);
    Bed_value->setText(QObject::tr("XXX-xxx-xx"));
    Bed_value->setGeometry(310,110,100,30);
    Bed_value->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Bed_value->setFont(font_line);

//####################
//创建氧气容量和气压那一行
//###################
    USE_lable=new QLabel(this);
    USE_lable->setText(QObject::tr("正在使用"));
    USE_lable->setGeometry(130,160,100,30);
    USE_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    USE_lable->setFont(font);

    USE_Pa=new QLabel(this);
    USE_Pa->setText(QObject::tr("XXXPa"));
    USE_Pa->setGeometry(130,200,100,30);
    USE_Pa->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    USE_Pa->setFont(font_line);

    USE_capacity=new QLabel(this);
    USE_capacity->setText(QObject::tr("XXXL"));
    USE_capacity->setGeometry(130,250,100,30);
    USE_capacity->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    USE_capacity->setFont(font_line);


    standby_lable=new QLabel(this);
    standby_lable->setText(QObject::tr("备      用"));
    standby_lable->setGeometry(250,160,100,30);
    standby_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    standby_lable->setFont(font);

    standby_Pa=new QLabel(this);
    standby_Pa->setText(QObject::tr("备用XXXPa"));
    standby_Pa->setGeometry(250,200,100,30);
    standby_Pa->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    standby_Pa->setFont(font_line);

    standby_capacity=new QLabel(this);
    standby_capacity->setText(QObject::tr("备用XXXL"));
    standby_capacity->setGeometry(250,250,100,30);
    standby_capacity->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    standby_capacity->setFont(font_line);

    Pa_lable=new QLabel(this);
    Pa_lable->setText(QObject::tr("压            强:"));
    Pa_lable->setGeometry(10,200,100,30);
    Pa_lable->setAlignment(Qt::AlignRight|Qt::AlignCenter);
    Pa_lable->setFont(font);

    capacity_lable=new QLabel(this);
    capacity_lable->setText(QObject::tr("氧气流量:"));
    capacity_lable->setGeometry(10,250,100,30);
    capacity_lable->setAlignment(Qt::AlignRight|Qt::AlignCenter);
    capacity_lable->setFont(font);

//####################
//创建显示按钮那一行
//###################
    switch_ptn=new QPushButton(this);
    switch_ptn->setText(QObject::tr("切换氧气设备"));
    switch_ptn->setGeometry(20,310,100,35);
    on_off_ptn=new QPushButton(this);
    on_off_ptn->setText(QObject::tr("氧气设备开关"));
    on_off_ptn->setGeometry(170,310,100,35);
    History_ptn=new QPushButton(this);
    History_ptn->setText(QObject::tr("历史记录"));
    History_ptn->setGeometry(320,310,100,35);
    this->setFixedSize(450,400);//设置窗口大小为固定大小

//####################
//状态栏信息
//###################
        message_left=new QLabel(this);
        message_left->setAlignment(Qt::AlignCenter);
        message_left->setFrameShape(QFrame::WinPanel);
        message_left->setFrameShadow(QFrame::Sunken);
        message_left->setText(QObject::tr("医疗氧气智能监控系统V1.0"));
        ui->statusbar->addWidget(message_left);

        message_right=new QLabel(this);
        message_right->setFrameStyle(QFrame::Box|QFrame::Sunken);
        message_right->setText(QObject::tr("测试版"));
        //message_right->setTextFormat(Qt::RichText);
       // message_right->setOpenExternalLinks(true);
        message_right->setMinimumSize(80,20);
        ui->statusbar->addPermanentWidget(message_right);




        timer=new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(DISPLAY()));
        timer->start(2000);
        connect(switch_ptn,&QPushButton::clicked,this,&Medical::switch_ptn_clicked);
        connect(on_off_ptn,&QPushButton::clicked,this,&Medical::on_off_ptn_clicked);
        connect(History_ptn,&QPushButton::clicked,this,&Medical::on_History_ptn_clicked);
}

Medical::~Medical()
{
    delete ui;
}
int Medical::DISPLAY()
{
    if(!CON_MYSQL_Patient())
    {
       if(Warning_State)
       {
           if(0==QMessageBox::warning(NULL,"CONNECT DATABASE ERROR","连接数据库失败，正在自动重连",QMessageBox::Ok) )
             Warning_State=1;
             else
             Warning_State=0;
        }
       return 0;
    }
    query=QSqlQuery(db);//对QSqlQuery::exec: database not open错误的解决方案,注意：：：一定要在连接之后才能调用，因为连接数据库之后，db1才是连接好的。
    query_P=QSqlQuery(db);

    if(!query_P.exec("select * from Patient_Login_Hospital"))
    {
        qDebug()<<"搜索出错"<<query_P.lastError();
        QMessageBox::warning(NULL,"SOURCH CLOUMNS ERROR","搜索出错",QMessageBox::Ok);
        return 0;
    }
    while(query_P.next())
    {
        qDebug()<<"DDDDD";
    qDebug()<<name_value<<Bed_value;
        if(Bed->currentText() ==query_P.value(8).toString())
        {
             name_value->setText(query_P.value(3).toString());
             Bed_value->setText(query_P.value(8).toString());
               qDebug()<<name_value->text()<<Bed_value->text();
             break;
        }

    } //设置姓名和床号




           if(!query.exec("select * from DATA"))
        {
            qDebug()<<"搜索出错"<<query.lastError();
            QMessageBox::warning(NULL,"SOURCH CLOUMNS ERROR","搜索出错",QMessageBox::Ok);
            return 0;
        }
        query.last();
        do{
            qDebug()<<"DDDDD";

            if( Bed_value->text() ==query.value(1).toString())
            {
                USE_Pa->setText(query.value(4).toString());
                USE_capacity->setText(query.value(6).toString());
            }}while(query.previous() );

         //设置姓名和床号

           // QMessageBox::warning(NULL,"INCROOECT","用户名或密码或账户类型不正确，请重新输入",QMessageBox::Ok);

    }

bool Medical::CON_MYSQL_Patient()
{

    db=QSqlDatabase::addDatabase("QMYSQL","CON_ME");
     qDebug() << "MYSQL driver valid" << db.isValid();
              db.setHostName("2001:da8:270:2018:f816:3eff:fe1d:bb01");
              db.setPort(3306);
              db.setDatabaseName("MQTT_DATA");
              db.setUserName("root");
              db.setPassword("lama940225");
              if(!db.open())
              {
                  qDebug()<<"打开数据库失败！"<<db.lastError();
                  return false;
              }

              return true;
}
void Medical::switch_ptn_clicked()
{
    switch_o=new Switch_verify();
    this->close();
    switch_o->show();
}

void Medical::on_off_ptn_clicked()
{
    on_off=new ON_Off_Verify();
    this->close();
    on_off->show();
}

void Medical::on_History_ptn_clicked()
{
    history=new History_Show();
    history->show();
}
